<template>

  <view>
    <view class="search-box">
      <my-search></my-search>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <!-- 跳转到详情页： navigator -->
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id'+item.goods_id">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航区域 -->
    <view class="nev-list">
      <view class="nev-item" v-for="(item,index) in navList" :key="index">
        <image :src="item.image_src" mode="" class="nav-img" @click="navClickHandler(item)"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <!-- 楼层的容器 -->
    <view class="floor-list">
      <!-- 每一个楼层的item项 -->
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层的标题 -->
        <image :src="item.floor_title.image_src" mode="" class="floor-img"></image>
        <!-- 楼层的图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧图片区域 -->
          <navigator class="floor-left-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="aspectFit"></image>
          </navigator>
          <!-- 右侧图片区域 -->
          <view class="floor-right-box">
            <navigator class="floor-right-item" v-for="(item1,index1) in item.product_list" :key="index1"
              v-if="index1!==0" :url="item1.url">
              <image :src="item1.image_src" :style="{width:item1.image_width+'rpx'}" mode="widthFix">
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
  </view>
</template>

<script>
  import minxBadge from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [minxBadge],
    data() {
      return {
        swiperList: [], //轮播图数据存储
        navList: [], //分类导航的数据
        floorList: [] //楼层的数据
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        //console.log(data)
        if (data.meta.status !== 200) {
          // uni.showToast({
          //   title: "获取失败",
          //   duration: 1500,
          //   icon: 'none'
          // })
          //调用封装的弹框方法
          uni.$showMsg()
        } else {
          this.swiperList = data.message
          uni.$showMsg('请求数据成功啦·~')
        }
      },
      async getNavList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (data.meta.status !== 200) {
          uni.$showMsg()
        }
        this.navList = data.message

      },
      //跳转到分类页面
      navClickHandler(item) {

        if (item.name === "分类") {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      //获取楼层数据
      async getFloorList() {
        const {
          data
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (data.meta.status !== 200) {
          return uni.$showMsg()
        }

        //对数据进行处理
        //先是遍历外层的message数组，获得每一个对象：就是每一层楼层
        data.message.forEach(floor => {
          //再是对每一层楼层里面的商品product_list进行遍历
          floor.product_list.forEach(prod => {
            //prod.navigator_url.split('?')[1] :意思是把prod.navigator_url用"?"分割，然后取索引为1的那个字符串拼接在一起
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split("?")[1]
          })
        })

        this.floorList = data.message
        console.log(data.message[0].product_list[0].url);
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 230px;

  }

  .swiper-item,
  image,
  {
  height: 100%;
  width: 100%;
  }

  .nev-list {
    display: flex;
    justify-content: space-around;
  }

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }

  .floor-img {
    height: 80rpx;
    width: 100%;
    display: flex;
  }

  .floor-img-box,
  .floor-left-box,
  floor-right-box {
    height: 390rpx;
  }

  .floor-right-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }

  .floor-img-box {
    display: flex;
    padding-left: 5rpx;
  }

  .search-box {
    position: sticky;
    top: 0px;
    z-index: 9999;
  }
</style>